package com.my.app.tools.core.service.translate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.my.app.tools.core.service.common.AppConfigService;
import com.my.app.tools.pojo.vo.GlobalResponseEntity;
import com.my.app.tools.pojo.vo.TranslateLangVO;
import com.my.app.tools.util.*;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TranslateBaiDuService extends AbstractTranslateApiService {

    private String translateUrl;
    private String translateAppId;
    private String translateSecretKey;

    @Override
    public void reloadAppConfig() {
        translateUrl = AppConfigService.readStringConfig("/app-config/translate-api/baidu/url");
        translateAppId = AppConfigService.readStringConfig("/app-config/translate-api/baidu/app-id");
        translateSecretKey = AppConfigService.readStringConfig("/app-config/translate-api/baidu/secret-key");
    }

    @Override
    public GlobalResponseEntity<TranslateLangVO[]> translateLang() {
        List<TranslateLangVO> langs = Lists.newArrayList();
        langs.add(new TranslateLangVO("zh", "en", "中文 >> 英文"));
        langs.add(new TranslateLangVO("zh", "jp", "中文 >> 日文"));
        langs.add(new TranslateLangVO("zh", "kor", "中文 >> 韩文"));
        langs.add(new TranslateLangVO("en", "zh", "英文 >> 中文"));
        langs.add(new TranslateLangVO("jp", "zh", "日文 >> 中文"));
        langs.add(new TranslateLangVO("kor", "zh", "韩文 >> 中文"));
        return GlobalResponseEntity.success("成功", langs.toArray(new TranslateLangVO[]{}));
    }

    /**
     * 文字翻译
     *
     * @param str
     * @param fromLang
     * @param toLang
     * @return
     * @author guochaohui
     * @date 2020-04-03 16:30
     */
    @Override
    public GlobalResponseEntity<List<String>> translate(String str, String fromLang, String toLang) {
        try {
            if (StringUtils.isBlank(str)) {
                return GlobalResponseEntity.success("空字符", "");
            }
            Map<String, String> params = buildTranslateParams(str, fromLang, toLang);
            String result = HttpGet.get(translateUrl, params);
            LogUtil.info("translate baidu result: " + result);
            JSONObject jsonObject = JSON.parseObject(result);
            JSONArray transResults = jsonObject.getJSONArray("trans_result");
            List<String> results = Lists.newArrayList();
            for (int i = 0; i < transResults.size(); i++) {
                JSONObject transResult = transResults.getJSONObject(i);
                results.add(transResult.getString("dst"));
            }
            return GlobalResponseEntity.success("翻译成功", results);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.error("translate baidu error: " + e.getMessage());
            return GlobalResponseEntity.failure(e.getMessage());
        }
    }

    /**
     * 构建翻译参数
     *
     * @param query
     * @param from
     * @param to
     * @return
     * @author guochaohui
     * @date 2020-04-03 16:30
     */
    private Map<String, String> buildTranslateParams(String query, String from, String to) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("q", query);
        params.put("from", from);
        params.put("to", to);
        params.put("appid", translateAppId);
        // 随机数
        String salt = String.valueOf(System.currentTimeMillis());
        params.put("salt", salt);
        // 签名
        String src = translateAppId + query + salt + translateSecretKey;
        params.put("sign", MD5Util.encrypt(src));
        return params;
    }

}
